import {getStubed} from '@/api/stubedApi'
import {addGroup, getGroupList, deleteGroup, updateGroup, getGroup, studentGroupAdd, studentGroupRemove, studentGroup} from '@/api/groupApi'
const group = {
    namespaced: true,
    state: { 
        addGroupVisible: false,
      
        groupTree: [],
        stubedList: [],
        PageNum: 1,
        PageSize: 10,
        total: 0,

        selectedGroup: []
    },
    getters: {

    },
    mutations: { 

    },
    actions: {
        getGroupList ({state, rootState, dispatch}, params) {
            getGroupList(params).then(res => {
                var children = [{
                    groupName: '默认',
                    id: 0
                }]
                state.groupTree = [{
                    groupName: rootState.dorm.curDorm.name,
                    children: res.items
                }]
            })
        },
        addGroup ({state}, params) {
            return new Promise((resolve, reject) => {
                addGroup(params).then(res => {
                    state.groupTree[0].children.push(res)
                    resolve()
                })
            })
        },
        deleteGroup ({state}, id) {
            return new Promise((resolve, reject) => {
                deleteGroup(id).then(res => {
                    var list = state.groupTree[0].children
                    for (let i = 0; i < list.length; i++) {
                        if (id == list[i].id) {
                            list.splice(i, 1)
                        }
                    }
                    resolve()
                })
            })
        },
        getStubed ({state, commit, dispatch, rootGetters, rootState}, params) {
            state.PageNum = params.PageNum
            if (state.PageNum < 1) {
                state.PageNum = 1
                return
            }
            
            var data = {
              SchoolId: rootGetters.cookiesObj.schoolId,
              PageNum: state.PageNum,
              PageSize: state.PageSize,
              Sorting: 'CreationTime Desc',
              DormitoryId: rootState.dorm.curDorm.id
             
            }
            getStubed(data).then(res => {
              state.stubedList = res.items
              state.total = res.totalCount
                if (res.items.length > 0) {
                   
                } else {
                    dispatch('getStubed', {PageNum: state.PageNum - 1})
                }
            })
        },
        studentGroup ({state, commit, dispatch, rootGetters}, params) {
            state.PageNum = params.PageNum
            if (state.PageNum < 1) {
                state.PageNum = 1
                return
            }

            params.PageSize = state.PageSize
            studentGroup(params).then(res => {
                state.stubedList = res.items
                state.total = res.totalCount
                if (res.items.length > 0) {
                   
                } else {
                    dispatch('studentGroup', {PageNum: state.PageNum - 1})
                }
            })
        },
        studentGroupAdd ({state, commit, dispatch, rootGetters}, params) {
            return new Promise((resolve, reject) => {
                studentGroupAdd(params).then(res => {
                    resolve()
                })
            })
        },
        studentGroupRemove ({state, commit, dispatch, rootGetters}, params) {
            return new Promise((resolve, reject) => {
                studentGroupRemove(params).then(res => {
                    resolve()
                })
            })
        }
    }
}
export default group
